from collections import namedtuple

PrimaryMeasureConf = namedtuple("PrimaryMeasureConf", "name addrs type unit ratio")
PRIMARY_MEA_CONFS = (
    PrimaryMeasureConf("A相电流", (0x0000, 0x0001), "float32", "A", 1),
    PrimaryMeasureConf("B相电流", (0x0002, 0x0003), "float32", "A", 1),
    PrimaryMeasureConf("C相电流", (0x0004, 0x0005), "float32", "A", 1),
    PrimaryMeasureConf("电流平均值", (0x0006, 0x0007), "float32", "A", 1),
    PrimaryMeasureConf("A相相电压", (0x0008, 0x0009), "float32", "V", 1),
    PrimaryMeasureConf("B相相电压", (0x000A, 0x000B), "float32", "V", 1),
    PrimaryMeasureConf("C相相电压", (0x000C, 0x000D), "float32", "V", 1),
    PrimaryMeasureConf("相电压平均值", (0x000E, 0x000F), "float32", "V", 1),
    PrimaryMeasureConf("AB线电压", (0x0010, 0x0011), "float32", "V", 1),
    PrimaryMeasureConf("BC线电压", (0x0012, 0x0013), "float32", "V", 1),
    PrimaryMeasureConf("CA线电压", (0x0014, 0x0015), "float32", "V", 1),
    PrimaryMeasureConf("线电压平均值", (0x0016, 0x0017), "float32", "V", 1),
    PrimaryMeasureConf("A相功率因数", (0x0018, 0x0019), "float32", "", 1),
    PrimaryMeasureConf("B相功率因数", (0x001A, 0x001B), "float32", "", 1),
    PrimaryMeasureConf("C相功率因数", (0x001C, 0x001D), "float32", "", 1),
    PrimaryMeasureConf("三相功率因数", (0x001E, 0x001F), "float32", "", 1),
    PrimaryMeasureConf("系统频率", (0x0020, 0x0021), "float32", "Hz", 1),
    PrimaryMeasureConf("A相有功功率", (0x0022, 0x0023), "float32", "W", 1),
    PrimaryMeasureConf("B相有功功率", (0x0024, 0x0025), "float32", "W", 1),
    PrimaryMeasureConf("C相有功功率", (0x0026, 0x0027), "float32", "W", 1),
    PrimaryMeasureConf("三相有功功率", (0x0028, 0x0029), "float32", "W", 1),
    PrimaryMeasureConf("A相无功功率", (0x002A, 0x002B), "float32", "Var", 1),
    PrimaryMeasureConf("B相无功功率", (0x002C, 0x002D), "float32", "Var", 1),
    PrimaryMeasureConf("C相无功功率", (0x002E, 0x002F), "float32", "Var", 1),
    PrimaryMeasureConf("三相无功功率", (0x0030, 0x0031), "float32", "Var", 1),
    PrimaryMeasureConf("A相视在功率", (0x0032, 0x0033), "float32", "VA", 1),
    PrimaryMeasureConf("B相视在功率", (0x0034, 0x0035), "float32", "VA", 1),
    PrimaryMeasureConf("C相视在功率", (0x0036, 0x0037), "float32", "VA", 1),
    PrimaryMeasureConf("三相视在功率", (0x0038, 0x0039), "float32", "VA", 1),
    PrimaryMeasureConf("正向有功电度", (0x003A, 0x003B), "float32", "kWh", 10),
    PrimaryMeasureConf("负向有功电度", (0x003C, 0x003D), "float32", "kWh", 10),
    PrimaryMeasureConf("有功电度总量", (0x003E, 0x003F), "float32", "kWh", 10),
    PrimaryMeasureConf("有功电度净值", (0x0040, 0x0041), "float32", "kWh", 10),
    PrimaryMeasureConf("正向无功电度", (0x0042, 0x0043), "float32", "kVarh", 10),
    PrimaryMeasureConf("负向无功电度", (0x0044, 0x0045), "float32", "kVarh", 10),
    PrimaryMeasureConf("无功电度总量", (0x0046, 0x0047), "float32", "kVarh", 10),
    PrimaryMeasureConf("无功电度净值", (0x0048, 0x0049), "float32", "kVarh", 10),
    PrimaryMeasureConf("Ia谐波畸变率", (0x004A, 0x004B), "float32", "%", 1),
    PrimaryMeasureConf("Ib谐波畸变率", (0x004C, 0x004D), "float32", "%", 1),
    PrimaryMeasureConf("Ic谐波畸变率", (0x004E, 0x004F), "float32", "%", 1),
    PrimaryMeasureConf("Ua谐波畸变率", (0x0050, 0x0051), "float32", "%", 1),
    PrimaryMeasureConf("Ub谐波畸变率", (0x0052, 0x0053), "float32", "%", 1),
    PrimaryMeasureConf("Uc谐波畸变率", (0x0054, 0x0055), "float32", "%", 1),
)

DIO_Conf = namedtuple("DIO_Conf", "name addrs type switch")
DIO_CONFS = (
    DIO_Conf("开入1状态", (0x0064,), "enum", ((0x0000, "分"), (0xFFFF, "合"))),
    DIO_Conf("开入2状态", (0x0065,), "enum", ((0x0000, "分"), (0xFFFF, "合"))),
    DIO_Conf("保护跳闸状态", (0x0066,), "enum", ((0x0000, "非保护跳闸"), (0xFFFF, "保护跳闸"))),
)

ParaConf = namedtuple("ParaConf", "name addrs type unit switch ratio")
PARA_CONFS = (
    ParaConf("接线方式", (0x00C8,), "enum", "", ((0x0000, "两表法"), (0xFFFF, "三表法")), None),
    ParaConf("PT变比", (0x00C9,), "uint16", "", None, 10),
    ParaConf("CT变比", (0x00CA,), "uint16", "", None, 1),
    # ParaConf("遥信去抖时间",  (0x00CB,),  "uint16", "mS", None, 1),
    ParaConf("电流显示门槛值", (0x00CC,), "uint16", "mA", None, 1),
    ParaConf("电压显示门槛值", (0x00CD,), "uint16", "mV", None, 1),
    # ParaConf("保护跳闸状态保持时间",  (0x00CE,),  "uint16", "mS", None, 1),
    ParaConf("设备地址", (0x00D6,), "uint16", "%", None, 1),
    ParaConf("波特率", (0x00D7,), "enum", "", ((1, "19200"), (2, "9600"), (3, "4800"), (4, "2400"), (5, "1200")), None),
)

INFO_CONFS = (
    ParaConf("软件版本号", (0x0190,), "uint16", "%", None, 1),
    ParaConf("软件版本日期", (0x0191,), "uint16", "%", None, 1),
    ParaConf("硬件版本号", (0x0192,), "uint16", "%", None, 1),
    ParaConf("液晶背光控制", (0x0193,), "wo_16", "", None, None),
    ParaConf("复位参数设置", (0x0194,), "wo_16", "", None, None),
    ParaConf("电度参数清零", (0x0195,), "wo_16", "", None, None),
)

ADJ_MEN_CONFS = (
    ParaConf("UA幅值", (0x01F4,), "uint16", "", None, 1),
    ParaConf("UB幅值", (0x01F5,), "uint16", "", None, 1),
    ParaConf("UC幅值", (0x01F6,), "uint16", "", None, 1),
    ParaConf("IA幅值", (0x01F7,), "uint16", "", None, 1),
    ParaConf("IB幅值", (0x01F8,), "uint16", "", None, 1),
    ParaConf("IC幅值", (0x01F9,), "uint16", "", None, 1),
    ParaConf("A相功率", (0x01FA,), "uint16", "", None, 1),
    ParaConf("B相功率", (0x01FB,), "uint16", "", None, 1),
    ParaConf("C相功率", (0x01FC,), "uint16", "", None, 1),
    ParaConf("A相相位", (0x01FD,), "uint16", "", None, 1),
    ParaConf("B相相位", (0x01FE,), "uint16", "", None, 1),
    ParaConf("C相相位", (0x01FF,), "uint16", "", None, 1),
    ParaConf("A相相位", (0x0200,), "uint16", "", None, 1),
    ParaConf("B相相位", (0x0201,), "uint16", "", None, 1),
    ParaConf("C相相位", (0x0202,), "uint16", "", None, 1),
)

ADJ_AUTO_CONFS = (
    ParaConf("自动校正模式控制", (0x1000,), "wo_16", None, None, None),
    ParaConf("自动校正操作", (0x1050,), "wo_16", None, None, None),
    ParaConf("自动校正状态", (0x1051,), "bit", None, None, None),
)

SecondaryMeasureConf = namedtuple("SecondaryMeasureConf", "name addrs type unit ratio format toleranceType")
SECONDARY_MEA_CONFS = (
    SecondaryMeasureConf("A相电流", (0x10CC,), "uint16", "A", 1000, ".3f", "percent"),
    SecondaryMeasureConf("B相电流", (0x10CD,), "uint16", "A", 1000, ".3f", "percent"),
    SecondaryMeasureConf("C相电流", (0x10CE,), "uint16", "A", 1000, ".3f", "percent"),
    SecondaryMeasureConf("电流平均值", (0x10CF,), "uint16", "A", 1000, ".3f", "percent"),
    SecondaryMeasureConf("A相相电压", (0x10D0,), "uint16", "V", 100, ".2f", "percent"),
    SecondaryMeasureConf("B相相电压", (0x10D1,), "uint16", "V", 100, ".2f", "percent"),
    SecondaryMeasureConf("C相相电压", (0x10D2,), "uint16", "V", 100, ".2f", "percent"),
    SecondaryMeasureConf("相电压平均值", (0x10D3,), "uint16", "V", 100, ".2f", "percent"),
    SecondaryMeasureConf("AB线电压", (0x10D4,), "uint16", "V", 100, ".2f", "percent"),
    SecondaryMeasureConf("BC线电压", (0x10D5,), "uint16", "V", 100, ".2f", "percent"),
    SecondaryMeasureConf("CA线电压", (0x10D6,), "uint16", "V", 100, ".2f", "percent"),
    SecondaryMeasureConf("线电压平均值", (0x10D7,), "uint16", "V", 100, ".2f", "percent"),
    SecondaryMeasureConf("A相功率因数", (0x10D8,), "int16", "", 1000, "+.3f", "absolute"),
    SecondaryMeasureConf("B相功率因数", (0x10D9,), "int16", "", 1000, "+.3f", "absolute"),
    SecondaryMeasureConf("C相功率因数", (0x10DA,), "int16", "", 1000, "+.3f", "absolute"),
    SecondaryMeasureConf("三相功率因数", (0x10DB,), "int16", "", 1000, "+.3f", "absolute"),
    SecondaryMeasureConf("系统频率", (0x10DC,), "uint16", "Hz", 100, ".2f", "absolute"),
    SecondaryMeasureConf("A相有功功率", (0x10DD, 0x10DE), "int32", "W", 1000, ".3f", "percent"),
    SecondaryMeasureConf("B相有功功率", (0x10DF, 0x10E0), "int32", "W", 1000, ".3f", "percent"),
    SecondaryMeasureConf("C相有功功率", (0x10E1, 0x10E2), "int32", "W", 1000, ".3f", "percent"),
    SecondaryMeasureConf("三相有功功率", (0x10E3, 0x10E4), "int32", "W", 1000, ".3f", "percent"),
    SecondaryMeasureConf("A相无功功率", (0x10E5, 0x10E6), "int32", "Var", 1000, ".3f", "percent"),
    SecondaryMeasureConf("B相无功功率", (0x10E7, 0x10E8), "int32", "Var", 1000, ".3f", "percent"),
    SecondaryMeasureConf("C相无功功率", (0x10E9, 0x10EA), "int32", "Var", 1000, ".3f", "percent"),
    SecondaryMeasureConf("三相无功功率", (0x10EB, 0x10EC), "int32", "Var", 1000, ".3f", "percent"),
    SecondaryMeasureConf("A相视在功率", (0x10ED, 0x10EE), "uint32", "VA", 1000, ".3f", "percent"),
    SecondaryMeasureConf("B相视在功率", (0x10EF, 0x10F0), "uint32", "VA", 1000, ".3f", "percent"),
    SecondaryMeasureConf("C相视在功率", (0x10F1, 0x10F2), "uint32", "VA", 1000, ".3f", "percent"),
    SecondaryMeasureConf("三相视在功率", (0x10F3, 0x10F4), "uint32", "VA", 1000, ".3f", "percent"),
    SecondaryMeasureConf("正向有功电度", (0x10F5, 0x10F6), "uint32", "kWh", 10, ".1f", None),
    SecondaryMeasureConf("负向有功电度", (0x10F7, 0x10F8), "uint32", "kWh", 10, ".1f", None),
    SecondaryMeasureConf("有功电度总量", (0x10F9, 0x10FA), "uint32", "kWh", 10, ".1f", None),
    SecondaryMeasureConf("有功电度净值", (0x10FB, 0x10FC), "uint32", "kWh", 10, ".1f", None),
    SecondaryMeasureConf("正向无功电度", (0x10FD, 0x10FE), "uint32", "kVarh", 10, ".1f", None),
    SecondaryMeasureConf("负向无功电度", (0x10FF, 0x1100), "uint32", "kVarh", 10, ".1f", None),
    SecondaryMeasureConf("无功电度总量", (0x1101, 0x1102), "uint32", "kVarh", 10, ".1f", None),
    SecondaryMeasureConf("无功电度净值", (0x1103, 0x1104), "uint32", "kVarh", 10, ".1f", None),
)

IDIN201F_READ_QUEUE = ((0x0000, 86), (0x0064, 3), (0x00C8, 16), (0x0190, 3), (0x01F4, 15), (0x1051, 1), (0x10CC, 57))


def idin201fGetWriteAreaName(addr):
    if addr in (0x00C8, 0x00D6):
        return "IDIN201F 参数"
    else:
        for unit in (PRIMARY_MEA_CONFS, PARA_CONFS, INFO_CONFS, ADJ_MEN_CONFS, ADJ_AUTO_CONFS, SECONDARY_MEA_CONFS):
            for conf in unit:
                if addr in conf.addrs:
                    return conf.name
    return ""
